package com.example.service;


import com.example.entity.Book;
import com.example.entity.Seat;
import com.example.entity.SeatRecord;
import com.example.exception.CustomerException;
import com.example.mapper.SeatMapper;
import com.example.mapper.SeatRecordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
public class SeatService {
    @Autowired
    private BookService bookService;
    @Autowired
    private SeatMapper seatMapper;
    @Autowired
    private SeatRecordMapper seatRecordMapper;
    @Autowired
    private SeatRecordService seatRecordService;
    public List<Seat> selectAll(Seat seat){
        return seatMapper.selectAll(seat);
    }

    public PageInfo<Seat> selectPage(Integer pageNum, Integer pageSize,Seat seat) {
        PageHelper.startPage(pageNum,pageSize);
        List<Seat> list = seatMapper.selectAll(seat);
        return PageInfo.of(list);

    }

    public void add(Seat seat) {
        seat.setStatus("未选");
        seatMapper.insert(seat);
    }

    public void update(Seat seat) {
        SeatRecord seatRecord=new SeatRecord();
        seatRecord.setNo(seat.getNo());
        seatRecord.setRoom(seat.getRoomName());
        seatRecord.setUserName(seat.getUserName());
        seatRecord.setDate((LocalDate.now()).toString());
        seatRecord.setSeatId(seat.getId());
        seatRecordMapper.insert(seatRecord);
        seatMapper.updateById(seat);
    }

    public void deleteById(Integer id) {
//        Book book = bookService.selectBySeatId(id);
//        if (book==null){
//            seatMapper.deleteById(id);
//        }else {
//            throw new CustomerException("该分类下存在书籍，无法删除");
//        }
        seatMapper.deleteById(id);
    }


    @Transactional
    public void deleteBatch(List<Integer> idList) {
        for (Integer id : idList) {
//            seatMapper.deleteById(id);
            this.deleteById(id);
        }
    }

        public void cancelById(Integer setId) {
        seatRecordMapper.deleteBySeatId(setId);
        seatMapper.cancelById(setId);
    }
}
